// packageUser/pages/disclosure-list/index.js
import {getDisclosures} from "../../../service/User";
import {downloadFile, openDocument} from "../../../service/Wx";
import Dialog from "@vant/weapp/dialog/dialog";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        isLoading: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDisclosureList();
    },

    /*
    * 获取披露信息
    * */
    getDisclosureList: async function () {
        try {
            let res = await getDisclosures();
            this.setData({
                list: res.map(function (item) {
                    return {
                        c_fund_name: item.c_fund_name,
                        // typeName: item.type === 1 ? "月度报表" : (item.type === 2 ? "季度报表" : "年度报表"),
                        file: item.file,
                        title: item.title ? item.title.split("_")[0] : "",
                        time: item.time.split(" ")[0]
                    }
                })
            })
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({isLoading: false})
        }
    },

    /*
    * 点击查看
    * */
    bindShowMore: async function(e){
        try {
            let idx = e.currentTarget.dataset.index;
            let url = this.data.list[idx].file;
            if (!url) {
                await Dialog.alert({
                    message: "该条信息没有上传相关文件！",
                })
                return false
            }
            let res = await downloadFile({url});
            await openDocument({
                filePath: res.tempFilePath,
                showMenu: true
            });
        } catch (e) {
            console.error(e)
        }
    }
})